﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using AutoMapper;
namespace AutoCodeUI
{
    public partial class Frm_EntityCode : Form
    {
        public Frm_EntityCode()
        {
            InitializeComponent();
        }
        
        public Frm_EntityCode(IServiceProvider serviceProvider, string @namespace, string entityFileName,string path)
        {
            this.serviceProvider = serviceProvider;
            Namespace = @namespace;
            EntityFileName = entityFileName;
            InitializeComponent();
            tb_namespace.Text = Namespace;
            tb_EntityFileName.Text = EntityFileName;
            tb_outpath.Text = path + @"\" + EntityFileName+".cs";
            List<string> l = new List<string>();
            
            
        }

        public IServiceProvider serviceProvider;
        public string Namespace;
        public string EntityFileName;
        ExportHelper export = new ExportHelper();
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.DefaultExt = "Xml";
            saveFile.Filter="所有文件|*.*|Xml|*.Xml";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                
                
                export.DataGridViewToXml(DataRow, saveFile.FileName);
   
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "所有文件|*.*|Xml|*.Xml";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                 export.Xml2DataGridView(DataRow, openFile.FileName);
                //System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
                //xd.Load(openFile.FileName);
                //System.Xml.XmlNode c = xd.SelectSingleNode("/NewDataSet/dataGridView1");

                //var mm =( (System.Xml.XmlElement)c).GetElementsByTagName("EntityName").Item(0).InnerText;
                //    var s = c.ChildNodes[0].Attributes["EntityName"].Value.ToString();
                //    string d = "a";
               
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Row r = new Row();
           
            List<KeyValuePair<string, object>> Listparams = new List<KeyValuePair<string, object>>();
            
            KeyValuePair<string, object> param = new KeyValuePair<string, object>("para","abc");
            KeyValuePair<string, object> para = new KeyValuePair<string, object>("dv",GetList());
            Listparams.Add(param);
            Listparams.Add(para);
            
            CodeService service = new CodeService(serviceProvider);
            service.OutputText(tb_template.Text, tb_outpath.Text,Listparams);
        }
        public RowList GetList()
        {
            try
            {
                RowList lists = new RowList();
                lists.namespaceText = tb_namespace.Text;
                lists.EntityName = tb_EntityFileName.Text;
                lists.interfaceText = textBox1.Text;
                lists.KeyType = textBox2.Text;
                foreach (DataGridViewRow dr in DataRow.Rows)
                {


                    Row t = new Row();
                    t.EntityName = dr.Cells["EntityName"].Value != null ? dr.Cells["EntityName"].Value.ToString() : "EntityName" + dr.Index;
                    t.EntityType = dr.Cells["EntityType"].Value != null ? dr.Cells["EntityType"].Value.ToString() : "string";
                    t.IsKey = dr.Cells["IsKey"].Value != null ? bool.Parse(dr.Cells["IsKey"].Value.ToString()) : false;
                    t.EntityValue = dr.Cells["EntityValue"].Value != null ? dr.Cells["EntityValue"].Value.ToString() : null;
                    t.MaxLenght = dr.Cells["MaxLenght"].Value != null ? dr.Cells["MaxLenght"].Value.ToString() : "50";
                    t.MinLenght = dr.Cells["MinLenght"].Value != null ? dr.Cells["MinLenght"].Value.ToString() : "0";
                    t.IsRequired = dr.Cells["IsNull"].Value != null ? bool.Parse(dr.Cells["IsNull"].Value.ToString()) : false;//默认可为空
                    t.IsForeignKey =dr.Cells["IsForeignKey"].Value!=null? bool.Parse(dr.Cells["IsForeignKey"].Value.ToString()):false;
                    t.TagText = dr.Cells["TagText"].Value != null ? dr.Cells["TagText"].Value.ToString() : "";
                    lists.rows.Add(t);
                }
                return lists;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DataRow.Rows.Add();
        }

        private void Frm_EntityCode_Load(object sender, EventArgs e)
        {
            
        }

        private void DataRow_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
    }
}
